package com.flute.icrawler.framework.framework.container;

import com.flute.icrawler.framework.framework.CrawlUrl;
import com.flute.tools.data.berkeleydb.BerkeleyDatabase;

public class BerkeleyUnSortedUrlQueue {

	private BerkeleyDatabase urlDatabase;
	private BerkeleyDatabase keyDatabase;
	private int size;

	public BerkeleyUnSortedUrlQueue(String env, String dbName) {

		this.keyDatabase = BerkeleyManager.getBerkeleyDatabase(env, dbName
				+ "key", new UrlNormalBinding(), new UrlKeyBinding());
		this.urlDatabase = BerkeleyManager.getBerkeleyDatabase(env, dbName,
				new UrlKeyBinding());

		size = Long.valueOf(urlDatabase.size()).intValue();

	}

	public synchronized void addCrawlUrl(CrawlUrl url) {
		url.resetProcessingInfos();
		if (keyDatabase.contains(url)) {
			CrawlUrl storedUrl = (CrawlUrl) keyDatabase.search(url);
			if (storedUrl.getAdditionalUrlInformation().compareTo(
					url.getAdditionalUrlInformation()) > 0) {

				keyDatabase.remove(url);
				keyDatabase.push(url);

				urlDatabase.remove(storedUrl);
				urlDatabase.push(url);
			}
		} else {
			keyDatabase.push(url);
			urlDatabase.push(url);
			size++;
		}
		flush();

	}

	public synchronized CrawlUrl getFirstCrawlUrl() {

		CrawlUrl url = (CrawlUrl) urlDatabase.pop();
		keyDatabase.remove(url);
		size--;

		// System.out.println(url.getUrl());
		// urlDatabase.printContent();

		return url;
	}

	public synchronized int size() {

		return size;
	}

	public boolean contains(CrawlUrl url) {
		return keyDatabase.contains(url);
	}

	public void clear() {
		size = 0;
		urlDatabase.clear();
		keyDatabase.clear();
	}

	public void flush() {
		urlDatabase.flush();
		keyDatabase.flush();
	}
}
